package com.atguigu.c_collections_sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Demo {
    public static void main(String[] args) {

        List<Integer> list1 = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);//此方法得到的集合是不可变!


        System.out.println("list1 = " + list1);

//        list1.add(10);
//        System.out.println("list1 = " + list1);

        //创建ArrayList集合
        ArrayList<Student> list = new ArrayList<>();

        Collections.addAll(list,
                new Student("张三",18),
                new Student("李四",17),
                new Student("王五",19));

        list.add(new Student("赵六",20));

        //对其进行排序
        Collections.sort(list); //如果没有提供排序规则,直接编译报错

        System.out.println("list = " + list);


        //独立比较器实现排序规则的修改
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println("list = " + list);
    }
}
